#include "user_extern.h"

#define AIXIN_W 32
#define BMP_POS_Y_DOWN (STRING_POS_Y_DOWN / 8)
uint8_t pos_offset = 0; /* 向左位移 */
uint8_t m1 = 0, m2 = 1;

static int start_count(int cur, int max, int div)
{
    return (cur * div / max);
}
/****************************************
 *  名称： digimon_display
 *  功能： digimon 在主界面的展示
 *  参数： digimon_t *digi 要显示的数码宝贝
 *  返回值：DIGI_VOID
 ****************************************/
DIGI_VOID digimon_display(uint8_t m_tiki)
{
    if (m_tiki)
    {
        ui.draw_digimon(40 - (pos_offset / 2), DIGIMON_Y, digimon.bmp[m1]);
    }
    else
    {
        ui.draw_digimon(40 - (pos_offset / 2), DIGIMON_Y, digimon.bmp[m2]);
    }
}

/****************************************
 *  名称： digimon_dis_menu
 *  功能： digimon 在主界面的展示 s菜单图标
 *  参数： uint8_t index 图标序号
 *  返回值：DIGI_VOID
 ****************************************/
DIGI_VOID digimon_dis_menu(uint8_t index)
{
#if 0
    oled_show_sys_bmp(55, 8, digi_sys_menu_select);
	uint8_t i = 0;
	for(i = 0; i < 5; i++)
	{
        if((index -2 + i) > 0)
        {
          //  oled_draw_bmp(25*i+5, 8, 2, 16, (void *)digi_sys_array[index+1]);
            oled_show_sys_bmp(25*i+5, 8, digi_sys_array[index-2+i]);//
        }
		
	}
    digi_system.sys_menu_lock = SYS_LOCK;
#endif
    ui.draw_sys_bmp(0, 8, index);
}
/* 显示爱心 */
DIGI_VOID digimon_dis_aixin(uint8_t current)
{
    int i = 0;
    while (i < 4)
    {
        if (i < start_count(current, 100, 4))
            ui.draw_sys_xin((AIXIN_W * i), BMP_POS_Y_DOWN, 1); /* 显示实心 */
        else
            ui.draw_sys_xin((AIXIN_W * i), BMP_POS_Y_DOWN, 0); /* 显示空心 */
        i++;
    }
}

static DIGI_VOID _digimon_dis_shirt(uint8_t index, uint8_t num)
{
    if (index == 0)
    {
        if (num >= 3)
        {
            ui.draw_digi_shirt(63, 4, 1);
        }
        if (num >= 2)
        {
            ui.draw_digi_shirt(95, 0, 1);
        }
        ui.draw_digi_shirt(95, 4, 1);
    }
    else
    {
        if (num >= 3)
        {
            ui.draw_digi_shirt(63, 4, 0);
        }
        if (num >= 2)
        {
            ui.draw_digi_shirt(95, 0, 0);
        }
        ui.draw_digi_shirt(95, 4, 0);
    }
}

DIGI_VOID digimon_dis_shirt(uint8_t m_tiki)
{
    m1 = 4;
    m2 = 5;
    (digimon.digi_dabian > 2) ? (pos_offset += 36 * 2) : (pos_offset += 36);
    if (m_tiki)
    {
        _digimon_dis_shirt(0, digimon.digi_dabian);
    }
    else
    {
        _digimon_dis_shirt(1, digimon.digi_dabian);
    }
}
static DIGI_VOID _digimon_dis_ill(uint8_t num)
{
    if (num <= 1)
    {
        ui.draw_digi_ill(96, 0);
    }
    else
    {
        ui.draw_digi_ill(64, 0);
    }
}
DIGI_VOID digimon_dis_ill(DIGI_VOID)
{
    m1 = 9;
    m2 = 10;
    if (digimon.digi_dabian == 2)
        pos_offset += 36;
    _digimon_dis_ill(digimon.digi_dabian);
}

/* 拒绝 */
void digimon_say_no(void)
{
    //struct digimon_st *digimon = digi;
    uint8_t index = 0;
    while (1)
    {
        if ((index % 2) == 0)
        {
            oled.draw_bmp(40, 6, 6, 48, BMP_COVER_MODE, (void *)digimon.bmp[6]);
        }
        else
        {
            oled.draw_bmp_reversal(40, 6, 6, 48, BMP_COVER_MODE, (void *)digimon.bmp[6]);
        }
        index++;
        oled.refresh_all(UI_CLEAN_ON);
        delay_ms(300);
        if (index > 6)
            break;
    }
}

uint8_t digimon_eat(struct digimon_st *digimon, uint8_t food)
{
    const unsigned char(*food_tmp)[32][4] = NULL;
    uint8_t index = 0;
    static uint8_t half = 0;
    if (food == FOOD_DRUMSTICKS)
    {
        food_tmp = digi_sys_food_jitui;
    }
    else if (food == FOOD_POTION)
    {
        food_tmp = digi_sys_food_dp;
    }

    while (1)
    {
        if (index == 0)
        {
            oled.draw_bmp(40, 6, 6, 48, BMP_COVER_MODE, (void *)digimon->bmp[0]);
            oled.draw_bmp(0, 0, 4, 32, BMP_COVER_MODE, (void *)food_tmp[0]);
        }
        else
        {
            if ((index % 2) == 0)
                oled.draw_bmp(40, 6, 6, 48, BMP_COVER_MODE, (void *)digimon->bmp[3]);
            else
                oled.draw_bmp(40, 6, 6, 48, BMP_COVER_MODE, (void *)digimon->bmp[2]);

            oled.draw_bmp(0, 4, 4, 32, BMP_COVER_MODE, (void *)food_tmp[index / 2]);
        }
        oled.refresh_all(UI_CLEAN_ON);
        index++;
        if (index > 6)
        {
            break;
        }
        delay_ms(450);
    }

    if (food == FOOD_DRUMSTICKS)
    {
        digimon->digi_hungry += 25;
        if (digimon->digi_hungry >= 100)
        {
            digimon->digi_hungry = 100;
            status_set_falg(digimon->digi_status, STA_FULL_HUNGRY);
        }
    }
    else if (food == FOOD_POTION)
    {
        digimon->digi_weight += 2;
        half++;
        if (half >= 2)
        {
            digimon->digi_dp++;
            half = 0;
        }
        if (digimon->digi_dp >= digimon->digi_maxdp)
        {
            digimon->digi_dp = digimon->digi_maxdp;
            status_set_falg(digimon->digi_status, STA_FULL_DP);
        }
    }
    else if (food == FOOD_MEDICINE)
    {
        status_del_flag(digimon->digi_status, STA_ILL);
        status_set_falg(digimon->digi_status, STA_NORMAL);
    }
    else if (food == FOOD_HURT_MED)
    {
        status_del_flag(digimon->digi_status, STA_HURT);
        status_set_falg(digimon->digi_status, STA_NORMAL);
    }

    return 1;
}
